const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const html = ['index', 'login', 'classesAdd', 'classesList', 'directorsAdd', 'subjectsAdd', 'subjectsList', 'studentsList', 'studentsAdd','studentsUpdate','register']
function HtmlPlugin() {
  return html.map(item =>
    new HtmlWebpackPlugin({
      template: `./src/${item}.html`,
      filename: `${item}.html`,
      chunks: [item],//在这个html里需要引入的js名
    })
  )
}
function entryPath() {
  const entry = {}
  html.forEach(item => {
    entry[item] = `./src/js/${item}.js`
  })
  return entry;
}
module.exports = {
  mode: 'development',
  entry: {
    ...entryPath()
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/[name].js'
  },
  module: {
    rules: [
      //css打包规则
      {
        test: /\.css$/i,//配置需要打包的文件名
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
        exclude: /node_modules/,//排除这个文件
      },
      //scss打包规则
      {
        test: /\.scss$/i,//配置需要打包的文件名
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
        exclude: /node_modules/,//排除这个文件
      },
      //css中的图片打包规则
      {
        test: /\.(png|jpg|jpeg|webp|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024 * 8, // 8kb 以下的图片都采用 base64 处理
              outputPath: './images/', // 图片的出口路径
              esModule: false
            }
          }
        ],
        exclude: /node_modules/,  // 排除 node_modules 文件
      },
      // html 中的图片
      {
        test: /\.html$/i,
        use: ['html-withimg-loader']
      }

    ]
  },
  plugins: [
    //jq
    new webpack.ProvidePlugin({
      '$': 'jquery'
    }),
    //打包css
    new MiniCssExtractPlugin({
      filename: './css/[name].css'
    }), ...HtmlPlugin()
  ],
  resolve: {
    alias: {
      '@css': path.resolve(__dirname, './src/css'),
      '@js': path.resolve(__dirname, './src/js'),
      '@utils': path.resolve(__dirname, './src/utils'),
    }
  },
  devServer: {
    openPage: 'index.html',  // 默认打开的页面
    port: 8888, // 设置端口号
    open: true,// 启动服务器时自动打开页面
    //ajax请求的服务地址
    // proxy: {
    //   '/': {
    //     target: 'http://localhost:3000'
    //   }
    // }
  }


};